"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var project_config_json_1 = __importDefault(require("../project.config.json"));
var https_1 = __importDefault(require("https"));
var querystring_1 = __importDefault(require("querystring"));
var stream_1 = require("stream");
var baseHeaders = {
    accept: 'application/json, text/javascript, */*; q=0.01',
    'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'accept-language': 'zh-CN,zh;q=0.9',
    pragma: 'no-cache',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36',
    referer: 'https://pc.woozooo.com/mydisk.php?item=files&action=index&u=1702063',
    'origin': 'https://pc.woozooo.com',
};
exports.baseHeaders = baseHeaders;
function parseJson(str) {
    try {
        return JSON.parse(str);
    }
    catch (e) {
        return str;
    }
}
var cookie = project_config_json_1.default.cookies[0];
function request(_a) {
    var body = _a.body, onData = _a.onData, url = _a.url, params = __rest(_a, ["body", "onData", "url"]);
    return new Promise(function (resolve, reject) {
        var fullUrl = url || "" + project_config_json_1.default.uploadUrl + params.path;
        var u = new URL(fullUrl);
        var headers = __assign(__assign(__assign({}, baseHeaders), { cookie: cookie }), params.headers);
        var options = __assign({ method: 'post', path: u.pathname, protocol: u.protocol, host: u.host }, params);
        var data;
        if (body) {
            if (isFormData(body)) {
                Object.assign(headers, body.getHeaders());
            }
            else {
                data = querystring_1.default.stringify(body);
            }
        }
        var req = https_1.default.request(__assign(__assign({}, options), { headers: headers }), function (res) {
            var data = '';
            res.setEncoding('utf8');
            res.on("data", function (chunk) {
                data += chunk;
            });
            res.on("end", function () {
                var json = parseJson(data);
                resolve(json);
            });
            res.on("error", function (err) {
                reject(err);
            });
        });
        req.on("error", function (err) {
            console.log("req error:", err);
        });
        if (!isFormData(body)) {
            req.write(data);
            req.end();
        }
        else {
            if (typeof onData === "function") {
                var bytes_1 = 0;
                body.on('data', function (chunk) {
                    bytes_1 += chunk.length;
                    onData(bytes_1);
                });
            }
            body.pipe(req);
        }
    });
}
function isFormData(body) {
    return body instanceof stream_1.Stream;
}
module.exports = request;
